package com.br.negocio.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.br.negocio.vo.CorVO;

@Service("corDAO")
public class CorDAO {

	@PersistenceContext
	private EntityManager entityManager;

	@Transactional(propagation = Propagation.REQUIRED)
	public void inserirCor(CorVO corVO) {
		try {
			entityManager.persist(corVO);
		} catch (Exception e) {
			throw new RuntimeException("Erro ao inserir cor: " + e.getMessage(), e);
		}
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	public void atualizarCor(CorVO corVO) {
		try {
			entityManager.merge(corVO);
		} catch (Exception e) {
			throw new RuntimeException("Erro ao atualizar cor: " + e.getMessage(), e);
		}
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void removerCor(CorVO corVO) {
		try {
			if (!entityManager.contains(corVO)) {
				corVO = entityManager.merge(corVO);
			}
			entityManager.remove(corVO);
		} catch (Exception e) {
			throw new RuntimeException("Erro ao remover cor: " + e.getMessage(), e);
		}
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public CorVO obterCor(CorVO corVO) {
		try {
			return entityManager.find(CorVO.class, corVO.getCodigo());
		} catch (Exception e) {
			throw new RuntimeException("Erro ao buscar cor: " + e.getMessage(), e);
		}
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.REQUIRED)
	public List<CorVO> obterListaCor() {
		try {
			List<CorVO> lista = (List<CorVO>) entityManager.createNamedQuery("loadAllCor").getResultList();
			if ((lista != null) && (!lista.isEmpty())) {
				return lista;
			} else {
				return Collections.EMPTY_LIST;
			}
		} catch (Exception e) {
			throw new RuntimeException("Erro ao carregar todos os cors.", e);
		}
	}
	
	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean validaDuplicado(CorVO corVO) {
		Query query = entityManager.createNamedQuery("findDuplicatedCor");
		query.setParameter("descricao", corVO.getDescricao());
		List<CorVO> lista = query.getResultList();
		if (lista.isEmpty()) {
			return true;
		} else {
			return false;
		}
	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
	
	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.REQUIRED)
	public ArrayList<CorVO> obterListaCorPdf() {
		try {
			ArrayList<CorVO> lista = (ArrayList<CorVO>)entityManager.createNamedQuery("loadAllCor").getResultList();
			if ((lista != null) && (!lista.isEmpty())) {
				return lista;
			} else {
				return null;
			}
		} catch (Exception e) {
			throw new RuntimeException("Erro ao carregar todos os cores.", e);
		}
	}

}